package com.example.demo.filter;

import com.example.demo.util.JSONResult;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <h3>reqister</h3>
 * <p>自定义用户校验的过滤器</p>
 *AbstractAuthenticationProcessingFilter
 *
 *
 * @author : chenjunjie
 * @date : 2021-06-23 09:00
 **/
public class UpdateUserFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
         String updateUserUrl="/Back/updateAccount";
        // 对登录url不进行验证码校验，直接通过
        if(updateUserUrl.equals(request.getRequestURI())) {

           if("".equals(request.getParameter("password"))||"".equals(request.getParameter("username"))||
           "".equals(request.getParameter("realName"))
           ){
               unsuccessfulAuthentication(response);
               return;
           }
        }
        filterChain.doFilter(request, response);
    }

    protected void unsuccessfulAuthentication(HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(JSONResult.fillResultString(HttpStatus.BAD_REQUEST.value(), "修改失败，字段不能为空", JSONObject.NULL));

    }
}
